const { Tab } = require('../../assets/libs/zanui/index');

var app = getApp();
Page(Object.assign({}, Tab, {
  data: {
    bannerList: [],
    archivesList: [],
    loading: false,
    nodata: false,
    nomore: false,
    pinglun:'',
    tab: {
      list: [],
      selectedId: '0',
      scroll: true,
      height: 44
    },
      tousuId:'',
      is_tousu:true,
      open: [
          {bindtap:'Tousu',txt:'投诉'},
          {bindtap:'Jubao',txt:'举报'},
      ],
      actionTousuHidden:true,
      actionSheetHidden:true,
      actionSheetItems:[
          {bindtap:'Menu',txt:'垃圾营销'},
          {bindtap:'Menu',txt:'涉黄信息'},
          {bindtap:'Menu',txt:'有害信息'},
          {bindtap:'Menu',txt:'违法信息'},
          {bindtap:'Menu',txt:'侵犯人身权益'},
      ],
      menu:'',
  },
  model: 1,
  channel: 0,
  page: 1,
  onLoad: function () {
    var that = this;
    this.page = 1;
    this.setData({ ["tab.list"]: app.globalData.indexTabList });

      app.request('/my/aboutus', function (data) {
          var content = data.pageInfo.content;
          data.pageInfo.article = app.towxml.toJson(content, 'html');
          that.setData({
              "modal.hidden": wx.getStorageSync("wan_hidden")?wx.setStorageSync("wan_hidden"):!1,
              "modal.title": data.pageInfo.title,
              "modal.msg": data.pageInfo.content,
              "modal.type": "success",
              "modal.params": {
                  type: "success"
              }
          });
      });

    app.request('/index/index', {}, function (data, ret) {
        var vote = wx.getStorageSync("vote") || [];
        for (var index in data.archivesList) {
            if (vote.indexOf(data.archivesList[index].id)>-1){
                data.archivesList[index].is_likes = 1;
            }
        }

      that.setData({
        bannerList: data.bannerList,
        archivesList: data.archivesList,
        tabList2: data.tabList2,
        tabList2_count:data.tabList2_count,
        ["tab.list"]: data.tabList,
        ["tab.selectedId"]: data.tabList[0].id,
      });
      that.channel = data.tabList[0].id;
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
    vote: function (event) {
        var that = this;
        var archivesIndex = event.currentTarget.dataset.index;

        app.vote(event, function (data) {
            that.data.archivesList[archivesIndex].likes = data.likes;
            that.data.archivesList[archivesIndex].is_likes = 1;
            that.setData({ ['likes']:  1,'archivesList':that.data.archivesList});
        });
    },
    collection: function (event) {

        var that = this;
        var archivesIndex = event.currentTarget.dataset.index;

        app.collections(event, function (data) {
            that.data.archivesList[archivesIndex].collection = data.collection;
            that.setData({ ['collections']: 1,'archivesList':that.data.archivesList});
        });
    },
    follow: function (event) {
        var that = this;
        var id = event.currentTarget.dataset.id;
        app.request("/user/follow", { follow_id: id }, function (data, ret) {
            if(data.code === 1){
                app.success('关注成功');
            }
            wx.setStorageSync("wan_hidden", 1);
            that.onLoad();
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },
    qu_follow: function (event) {
        var that = this;
        var id = event.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要取消关注吗？',
            success:function(res){
                if(res.confirm){
                    app.request("/user/qu_follow", { follow_id: id }, function (data, ret) {
                        if(data.code === 1){
                            app.success('取消关注成功');
                        }
                        wx.setStorageSync("wan_hidden", 1);
                        that.onLoad();
                    }, function (data, ret) {
                        app.error(ret.msg);
                    });
                }else{}
            }
        })
    },
    see: function (e) {
        var see_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../see/index?see_id='+see_id
        })
    },
    goDetail: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../news/detail?id='+id
        })
    },
    /**
     * 弹窗
     */
    showDialogBtn: function (event) {
        this.data.model = event.currentTarget.dataset.model_id;

        var aid = event.currentTarget.dataset.id;
        this.setData({
            aid: aid,
            showModal: true
        })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {
    },
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
        this.setData({
            showModal: false
        });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function () {
        this.hideModal();
    },
    /**
     * 获取评论内容
     */
    getPinglun: function (e) {
        var val = e.detail.value;
        this.setData({
            pinglun: val
        });
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function (event) {
        var that = this;
        var pid = event.currentTarget.dataset.pid;
        if (!app.globalData.userInfo) {
            app.error('请登录后再评论');
            return;
        }
        if (this.data.pinglun == '') {
            app.error('内容不能为空');
            return;
        }
        app.request('/comment/post', { aid: this.data.aid, pid: 0, content: this.data.pinglun }, function (data) {
            app.success('发表成功!');
            that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
            if (that.data.commentList.length < 10) {
                that.page = 1;
            } else {
                that.data.commentList = that.data.commentList.slice(0, 10);
                that.page = 2;
            }
            that.onReachBottom();
        }, function (data, ret) {
            app.error(ret.msg);
        });
        this.hideModal();
    },
  onPullDownRefresh: function () {
    this.setData({ nodata: false, nomore: false });
    this.page = 1;
    this.loadArchives(function () {
      wx.stopPullDownRefresh();
    });
  },
  onReachBottom: function () {
    var that = this;
    this.loadArchives(function (data) {
      if (data.archivesList.length == 0) {
        app.info("暂无更多数据");
      }
    });
  },
  loadArchives: function (cb) {
    var that = this;
    if (that.data.nomore == true || that.data.loading == true) {
      return;
    }
    this.setData({ loading: true });
    app.request('/archives/index', { model: 0,channel: this.channel, page: this.page }, function (data, ret) {
      that.setData({
        loading: false,
        nodata: that.page == 1 && data.archivesList.length == 0 ? true : false,
        nomore: that.page > 1 && data.archivesList.length == 0 ? true : false,
        archivesList: that.page > 1 ? that.data.archivesList.concat(data.archivesList) : data.archivesList,
        tabList2: data.tabList2,
        tabList2_count:data.tabList2_count,
      });
      that.page++;
      typeof cb == 'function' && cb(data);
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    this.channel = selectedId;
    this.page = 1;
    this.setData({
      nodata: false,
      nomore: false,
      [`${componentId}.selectedId`]: selectedId
    });
    wx.pageScrollTo({ scrollTop: 0 });
    this.loadArchives();
  },
  onShareAppMessage: function () {
    return {
      title: '不忘初心的社交平台',
      desc: '久馆，不忘初心的社交平台',
      path: '/page/index/index'
    }
  },
    actionTousuTap:function(event){
        var id = event.currentTarget.dataset.id;
        this.data.tousuId = id;
        this.setData({
            actionTousuHidden:!this.data.actionTousuHidden,
        })
    },
    actionTousubindchange:function(){
        this.setData({
            actionTousuHidden:!this.data.actionTousuHidden
        })
    },
    bindTousu:function(){
        this.actionSheetTap();
        this.setData({
            actionTousuHidden:!this.data.actionTousuHidden
        })
    },
    bindJubao:function(){
        this.data.is_tousu = false;
        this.actionSheetTap();
        this.setData({
            actionTousuHidden:!this.data.actionTousuHidden
        })
    },
    actionSheetTap:function(event){
        this.setData({
            actionSheetHidden:!this.data.actionSheetHidden
        })
    },
    actionSheetbindchange:function(){
        this.setData({
            actionSheetHidden:!this.data.actionSheetHidden
        })
    },
    bindMenu:function(event){
        var txt = event.currentTarget.dataset.txt;
        var id = this.data.tousuId;

        if(this.data.is_tousu){
            app.request('/complaint/post', { aid: id, pid:0, content: txt}, function (data) {
                app.success('投诉成功!');
            }, function (data, ret) {
                app.error(ret.msg);
            });
        }else{
            app.request('/report/post', { aid: id, pid:0, content: txt}, function (data) {
                app.success('举报成功!');
            }, function (data, ret) {
                app.error(ret.msg);
            });
        }
        this.setData({
            menu:1,
            actionSheetHidden:!this.data.actionSheetHidden
        })
    },
}))